package com.huifan.demo.controller;

import com.alibaba.fastjson.JSON;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: liuchenhui
 * @create: 2019-07-31 20:41
 **/
@Controller
public class TestController {
    @RequestMapping("/ping")
    @ResponseBody
    public String ping() {
        System.out.println("ping");
        return "pong";
    }

    @RequestMapping("/test")
    @ResponseBody
    public String test(TaxParam param, RyxxParam ryxx) {
        String str = JSON.toJSONString(param) + JSON.toJSONString(ryxx);
        return str;
    }

    @RequestMapping(value = "/redirect/test1")
    @ResponseBody
    public String test1(String token, HttpServletResponse response) {
        System.out.println("test1设置cookie:" + token);
        response.addHeader("Set-Cookie", "waves=" + token + "; Path=/; HttpOnly");
        return token;
    }

    @RequestMapping(value = "/redirect/test2")
    public String test2() {
        System.out.println("test2");
        return "redirect:" + "http://mp.lch.com/index.html";
    }

    @RequestMapping(value = "/redirect/test3")
    @ResponseBody
    public String test3(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return "false";
        }
        for (int i = 0; i < cookies.length; i++) {
            if ("waves".equals(cookies[i].getName())) {
                String value = cookies[i].getValue();
                System.out.println("test3获取cookie:" + value);
                return cookies[i].getValue();
            }
        }
        return "ok";
    }
}
